<?php
namespace Home\Controller;

/**
 * ClassController
 * 小说分类信息
 */
class ClassController extends CommonController {
    /**
     * 小说分类列表
     * @return
     */
    public function index() {
        $result = $this->getPagination('Class',['is_del'=>0]);

        $this->assign('Class', $result['data']);
        $this->assign('rows_count', $result['total_rows']);
        $this->assign('page', $result['show']);
        $this->display();
    }

    /**
     * 添加小说分类页面
     */
    public function add(){
        $this->display();
    }

    /**
     * 添加小说分类操作
     */
    public function insert(){
        $Class_Model = M('class');
        $data = I('post.class');
        if (!isset($data)) {
            return $this->errorReturn('无效的操作！');
        }

        if($data['class_name'] == '' || !$data['class_name']){
            return $this->errorReturn('分类名称不能为空！');
        }

        //上传图标
        if($_FILES['cover_pic']['tmp_name']){
            $info = $this->imgUpload('class_pic');
            if(!$info){
                return $this->errorReturn('封面上传失败请重试！');
            }else{
                $data['cover_pic'] = '/Public/uploads/'.$info['cover_pic']['savepath'].$info['cover_pic']['savename'];
            }
        }

        $data['create_time'] = time();
        $data['update_time'] = time();
        $result = $Class_Model->add($data);
        if (!$result) {
            return $this->errorReturn('添加失败，稍后请重试！');
        }

        return $this->successReturn('添加分类成功！', U('Class/index'));
    }


    /**
     * 编辑小说分类页面
     * @return
     */
    public function edit() {
        if (!isset($_GET['id'])) {
            return $this->error('参数错误，稍后请重试！');
        }

        $Class = M('class')->getById($_GET['id']);

        $this->assign('class', $Class);
        $this->assign('id',$_GET['id']);
        $this->display();
    }

    /**
     * 更新小说分类信息
     * @return
     */
    public function update() {
        $Class_Model = M('class');
        $data = I('post.class');
        if(!isset($data)){
            return $this->errorReturn('无效的操作！');
        }

        if($data['class_name'] == '' || !$data['class_name']){
            return $this->errorReturn('分类名称不能为空！');
        }

        //上传封面
        if($_FILES['cover_pic']['tmp_name']){
            $info = $this->imgUpload('class_pic');
            if(!$info){
                return $this->errorReturn('封面上传失败请重试！');
            }else{
                $data['cover_pic'] = '/Public/uploads/'.$info['cover_pic']['savepath'].$info['cover_pic']['savename'];
            }
        }
        $data['update_time'] = time();
        $result = $Class_Model->where(['id'=>$data['id']])->save($data);
        if (!$result) {
            return $this->errorReturn('操作失败，请重试！');
        }

        return $this->successReturn('分类名称修改成功！', U('Class/index'));
    }

    //删除小说分类
    public function del(){
        $Class_Model = M('class');
        $id = I('post.id');
        if(!$id){
            $this->ajaxReturn(['code'=>-1,'msg'=>'参数错误，请重试']);
        }

        $result = $Class_Model->where(['id'=>$id])->setField('is_del',1);
        if($result){
            $this->ajaxReturn(['code'=>0,'msg'=>'删除成功!']);
        }else{
            $this->ajaxReturn(['code'=>-1,'msg'=>'删除失败，请重试!']);
        }
    }

}
